import React from 'react'
import { SimpleSelector } from './simpleSelector'

class ControlType {
    constructor(code, props) {
        this.code = code
        this.props = props
    }

    render() {
        throw 'not implemented.'
    }
}

class TextControl extends ControlType {
    constructor(props = {}) {
        super('text', props)
    }

    render() {
        return <input {...this.props} />
    }
}

class SelectorStep extends ControlType {
    constructor(props = {}) {
        super('selector', props)
    }

    render() {
        const { dataSource, filterDataSource, getItemValue,
            renderItem, initValue, onSelectItem, readOnly = false
        } = this.props

        return <SimpleSelector dataSource={dataSource} filterDataSource={filterDataSource}
            getItemValue={getItemValue} initValue={initValue} onSelectItem={onSelectItem}
            readOnly={readOnly} renderItem={renderItem} />
    }
}

export class ControlTypeProvider {
    static getAllControlTypes() {
        return [
            new TextControl(),
            new SelectorStep()
        ]
    }

    static getControlTypeByCode(code) {
        const controlTypeInstance = ControlTypeProvider.getAllControlTypes().find(type => type.code === code)
        if (controlTypeInstance) {
            return controlTypeInstance
        } else {
            throw `unkown conrol type by code: ${code}`
        }
    }
}